package com.demo2do.fms.admin.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.entity.security.KoalaSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.web.resolver.Secure;

/**
 * 
 * @author Wilson
 * 
 */
@Controller
@MenuSetting("menu-data")
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Value("#{config['shell.shutdown']}")
    private String shellShutdown;
	
    /**
     * @return
     */
    @RequestMapping("/home")
    public String home() {
        return "redirect:/profile";
    }
    
    /**
     * @return
     */
    @RequestMapping(value = "/basic-data", method = RequestMethod.GET)
    @MenuSetting("menu-data")
    public String enterBasicData(){
        return "home";
    }
    
    /**
     * @return
     */
    @RequestMapping(value = "/business", method = RequestMethod.GET)
    @MenuSetting("menu-business")
    public String enterBusiness(){
          return "redirect:/shipment-lines/search?target=search";
    }

    /**
     * @return
     */
    @RequestMapping(value = "/supplier-management", method = RequestMethod.GET)
    @MenuSetting("menu-supplier")
    public String enterSupplier(@Secure Login login) {

        if (login.isCommonStorageSupplierUser()){
            return "redirect:/common-storage-suppliers";
        }
        return "redirect:/sub-suppliers";
    }
    
    /**
     * @return
     */
    @RequestMapping(value = "/authorities", method = RequestMethod.GET)
    @MenuSetting("menu-authority")
    public String enterAuthorities() {
        return "redirect:/profile";
    }
    
    /**
     * @return
     */
    @RequestMapping(value = "/report-management", method = RequestMethod.GET)
    @MenuSetting("menu-report")
    public String enterReport() {
        return "redirect:/reports";
    }
    
    /**
     * @return
     */
    @RequestMapping("/example-search")
    public String search() {
        return "example-search";
    }
    
    /**
     * 
     * @param login
     * @return
     */
    @RequestMapping("/example-search-result")
    public String searchResult(@Secure Login login) {
        return "example-search-result";
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping("/example-koala-search")
    public @ResponseBody String searchResult() {
        
        HttpResult httpResult = new HttpResult();
        
        List<KoalaSearch> result = new ArrayList<KoalaSearch>();
        
        KoalaSearch koalaSearchA = new KoalaSearch(new Long(1), "选项A", "001");
        KoalaSearch koalaSearchB = new KoalaSearch(new Long(2), "选项B", "002");
        KoalaSearch koalaSearchC = new KoalaSearch(new Long(3), "选项C", "003");
        
        result.add(koalaSearchA);
        result.add(koalaSearchB);
        result.add(koalaSearchC);
        
        httpResult.success().data("id", "1")
                            .data("name", "选项A")
                            .data("code", "003");
        
        return JsonUtils.toJson(httpResult);
    }
    
    /**
     * 
     * @param login
     * @return
     */
    @RequestMapping("/example-koala-modal-search")
    public String modalSearch(@Secure Login login) {
        return "example-koala-modal-search";
    }
    
    /**
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/example-koala-modal-search", method = RequestMethod.POST)
    public @ResponseBody String onModalSearch(@Secure Login login) {
        
        HttpResult httpResult = new HttpResult();
        
        List<KoalaSearch> result = new ArrayList<KoalaSearch>();
        
        KoalaSearch koalaSearchA = new KoalaSearch(new Long(1), "选项A", "001");
        KoalaSearch koalaSearchB = new KoalaSearch(new Long(2), "选项B", "002");
        KoalaSearch koalaSearchC = new KoalaSearch(new Long(3), "选项C", "003");
        
        result.add(koalaSearchA);
        result.add(koalaSearchB);
        result.add(koalaSearchC);
        
        httpResult.success().data("list", result);
        
        return JsonUtils.toJson(httpResult);
    }
    
    /**
     * @return
     */
    @RequestMapping(value = "/fileupload-example", method = RequestMethod.GET)
    public ModelAndView exampleUpload() {
        ModelAndView modelAndView = new ModelAndView("fileupload-example");
        return modelAndView;
    }
    
    /**
     * @return
     */
    @RequestMapping("/error")
    public String error() {
    	logger.error("#error() - test print ERROR log!!!");
        return "login";
    }
    
    /**
     * @return
     * @throws Exception
     */
    @RequestMapping("/shutdown")
    public @ResponseBody String shutdown() throws Exception {
        Runtime.getRuntime().exec(shellShutdown);
        return "success";
    }
    
    /**
     * @return
     * @throws Exception
     */
    @RequestMapping("/keep-alive")
    public @ResponseBody String keepAlive() throws Exception {
        return "success" + DateFormatUtils.yearAsString();
    }
    
}
